package com.yimingkeji.mysql.repository;

import com.yimingkeji.mysql.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Date;
import java.util.List;

/**
 * @Describe:
 * @Auther: zhenlong.yang
 * @Date: 2018/12/1 20:47
 */
public interface UserRepository extends JpaRepository<User, Integer> {

    //select * from user where name=?1
    User findByName(String name);

    List<User> findAllByName(String name);

    //select * from user where name=?1 and age=?2
    User findByNameAndAge(String name, int age);

    //select * from user where name=?1 or age=?2
    User findByNameOrAge(String name, int age);

    //select * from user where name in(?1)
    List<User> findByNameIn(List<String> nameList);

    //select * from user where age>?2
    List<User> findByAgeGreaterThan(int age);

    //select * from user where available = 0
    List<User> findByAvailableFalse();

    //select * from user where createtime between ?1 and ?2
    List<User> findByCreateTimeBetween(Date start, Date end);

    //select * from user where available = 1 limit 10
    List<User> findTop10ByAvailableIsTrue();

    //select count(1) where age=?1
    long countByAge(int age);

    //原生sql查询
    @Query(value = "select * from user where name=?1 and age > ?2", nativeQuery = true)
    List<User> queryBySQL(String name, int age);
}
